package com.ffcs.web.controllers;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.daos.implementations.WxUserResponse;
import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.dto.UserInfo;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.services.AuthCodeService;
import com.ffcs.web.services.WxUserService;
import com.ffcs.web.utils.CommonConfig;
import com.ffcs.web.utils.HttpGetUtil;
import com.ffcs.web.utils.JsonUtil;

@Controller
public class WxUserController {

	private Logger logger = Logger.getLogger(WxUserController.class);
	//微信支付商户开通后 微信会提供appid和appsecret和商户号partner
	private static String appid = CommonConfig.getProperty("APPID");
	private static String appsecret = CommonConfig.getProperty("APPSECRET");
	private static String partner = CommonConfig.getProperty("PARTNER");
	//这个参数partnerkey是在商户后台配置的一个32位的key,微信商户平台-账户设置-安全设置-api安全
	private static String partnerkey = CommonConfig.getProperty("PARTNERKEY");
	//openId 是微信用户针对公众号的标识，授权的部分这里不解释
	//微信支付成功后通知地址 必须要求80端口并且地址不能带参数
	private static String notifyurl = CommonConfig.getProperty("NOTIFYURL");
	
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private AuthCodeService authCodeService;
	
	
	
	/**
	 * 判断是否已经登录
	 * @return
	 */
	@RequestMapping("isLogin")
	@ResponseBody
	public BaseResponse isLogin(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		if(null==request.getSession().getAttribute("wxUser")){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("is-not-login");
			return baseResponse;
		}else{
			baseResponse.setResultCode("0");
			baseResponse.setResultDesc("success");
			return baseResponse;
		}
	}
	
	/**
	 * 我的资料
	 * @param request
	 * @return
	 */
	@RequestMapping("myNewsData")
	@ResponseBody
	public WxUserResponse myNewsData(HttpServletRequest request){
		WxUserResponse baseResponse=new WxUserResponse();
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		baseResponse.setWxUser(wxUser);
		return baseResponse;
	}
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getUserData")
	@ResponseBody
	public WxUserResponse getUserData(HttpServletRequest request){
		WxUserResponse baseResponse=new WxUserResponse();
		String userId=request.getParameter("userId");
		WxUser wxUser=wxUserService.getUserByUserId(userId);
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		baseResponse.setWxUser(wxUser);
		return baseResponse;
	}
	
	/**
	 * 用户登录（绑定）
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("loginBind")
	public void loginBind(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String telphone=request.getParameter("telphone");
		String code=request.getParameter("code");
		String goPage=(String) request.getSession().getAttribute("goPage");
		String BASE_URL=CommonConfig.getProperty("BASE_URL");
		
		if(!authCodeService.isAuthCode(telphone, code)){
			logger.info("验证码错误，跳回绑定页面");
			response.sendRedirect(BASE_URL+"/bind.jsp?v="+new Date().getTime());
			return ;
		}
		logger.info("user="+request.getSession().getAttribute("wxUser"));
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		wxUserService.bindUser(wxUser,telphone);
		logger.info("登录绑定成功，页面跳往"+BASE_URL+goPage+".jsp");
		response.sendRedirect(BASE_URL+goPage+".jsp");
		return ;
	}
	
	/**
	 * 判断是否登录，登录就去绑定，为登录就去获取openid
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("goZuoXinPage")
	public void goFreeAsk(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		String BASE_URL=CommonConfig.getProperty("BASE_URL");
		String goPage=request.getParameter("goPage");
		
		logger.info("用户已登录。判断是否需要绑定");
		WxUser wu=(WxUser) request.getSession().getAttribute("wxUser");
		String userStatus=wxUserService.isAddUser(wu.getOpenid());
		if("1".equals(userStatus)){
			logger.info("wu.getOpenid()"+wu.getOpenid()+"用户已登录。但未绑定");
			request.getSession().setAttribute("goPage",goPage);
			response.sendRedirect(BASE_URL+"/bind.jsp?v="+new Date().getTime());
			return ;
		}else if("2".equals(userStatus)){
			logger.info("wu.getOpenid()"+wu.getOpenid()+"用户已登录。已绑定");
			response.sendRedirect(BASE_URL+goPage+".jsp");
			return ;
		}
		
	}	
	
	@RequestMapping("isQiandao")
	@ResponseBody
	public BaseResponse isQiandao(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		boolean b=wxUserService.isQiandao(wxUser);
		if(!b){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("weiQqiandao");
			return baseResponse;
		}
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("yiQiandao");
		return baseResponse;
	}
	
	@RequestMapping("gotoQiandao")
	@ResponseBody
	public BaseResponse gotoQiandao(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		wxUserService.gotoQiandao(wxUser);
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	@RequestMapping("gotoTuCao")
	@ResponseBody
	public BaseResponse gotoTuCao(HttpServletRequest request){
		String content=request.getParameter("content");
		BaseResponse baseResponse=new BaseResponse();
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		wxUserService.gotoTuCao(wxUser,content);
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	@RequestMapping("gotoTuCao2")
	@ResponseBody
	public BaseResponse gotoTuCao2(HttpServletRequest request){
		String content=request.getParameter("content");
		BaseResponse baseResponse=new BaseResponse();
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		wxUserService.gotoTuCao(wxUser,content);
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
}
